﻿using EasyAdmin.Application.Contracts.Books;
using EasyAdmin.Application.Contracts.Books.Dtos;
using EasyAdmin.Domain.Entities.Applications;
using EasyCore.App;
using EasyCore.Application;
using EasyCore.Excel;
using EasyCore.ObjectMapper;
using EasyCore.UnifyResult;
using EasyCore.UnifyResult.Exceptions;
using Microsoft.VisualBasic;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyAdmin.Application.Applications.Books
{
    public class BookAppService : AppService, IBookAppService
    {
        private ISqlSugarRepository<Book> _bookRepository { get; set; }
        public BookAppService(ISqlSugarRepository<Book> bookRepository)
        {
            _bookRepository = bookRepository;
        }

        public async Task<Book> CreateBook(CreateUpdateBookDto input)
        {
            if (input.Price == 5)
            {
                throw new BusinessException("价格不能等于5");
            }
            var book = input.MapTo<Book>(); //映射到实体类型
            await _bookRepository.InsertAsync(book);
            var temp = EasyApp.Cache.Get<Book>("Book");
            EasyApp.Cache.Add("Book", book, -1);
            return book;
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<MemoryStream> Export()
        {
            var excelMapper = new ExcelMapper(true);
            var sheetMapper = excelMapper.CreateDataTableSheetMapper("数据列表");
            DataTable dt = new DataTable();
            dt.Columns.Add("标题1", typeof(string));
            dt.Columns.Add("标题2", typeof(string));
            dt.Columns.Add("标题3", typeof(string));
            dt.Columns.Add("标题4", typeof(string));
            dt.Columns.Add("标题5", typeof(string));
            dt.Columns.Add("标题6", typeof(string));
            for (int index = 0; index < 6; index++)
            {
                var row = dt.NewRow();
                row["标题1"] = string.Format("列1_{0}", index + 1);
                row["标题2"] = string.Format("列2_{0}", index + 1);
                row["标题3"] = string.Format("列3_{0}", index + 1);
                row["标题4"] = string.Format("列4_{0}", index + 1);
                row["标题5"] = string.Format("列5_{0}", index + 1);
                row["标题6"] = string.Format("列6_{0}", index + 1);
                dt.Rows.Add(row);
            }

            sheetMapper.Map("序号", colWidth: 20, colValFunc: (index, item) => index.ToString());
            sheetMapper.Map("自定义1", colWidth: 20, colValFunc: (index, item) =>
            {
                if (item.Table.Columns.Contains("标题1")) return item["标题1"].ToString();
                return string.Empty;
            });
            sheetMapper.Map("自定义2", new List<ColumnOption<DataRow>> {
                new ColumnOption<DataRow>("标题2"),
                      new ColumnOption<DataRow>("标题3"),
            });
            sheetMapper.Map("自定义3", new List<ColumnOption<DataRow>> {
                new ColumnOption<DataRow>("标题4"),
                      new ColumnOption<DataRow>("标题5"),
            });
            var parentTitle = sheetMapper.Map("自定义3");
            parentTitle.AddChildColumn("标题4", columnWidth: 30);
            parentTitle.AddChildColumn("标题5", columnWidth: 30);

            sheetMapper.Map("标题6");
            sheetMapper.Map("abc", fieldName: "abc", colValFunc: (index, item) =>
            {
                return string.Empty;
            });

            sheetMapper.Put(dt);


            var sheetMapper2 = excelMapper.CreateEntitySheetMapper<BookDto>("书籍列表");
            List<BookDto> books = new List<BookDto>() {
            new BookDto(){ Name ="UML设计",Auther="张润发",PublishDate = DateTime.Now,Price = 58.6f },
              new BookDto(){ Name ="MVC开发",Auther="周星星",PublishDate = DateTime.Now.AddDays(-7),Price = 100f }
            };
            sheetMapper2.Put(books);



            MemoryStream ms = excelMapper.ToMemoryStream();
            return ms;
        }

    }
}
